<template>
  <div class="orderChart">
    <div id="orderChart" style="width: 100%;height: 100%;"></div>
  </div>
</template>

<script>
  export default {
    data(){
      return {
        echartsNames: [],
        dataList: [[], [], [], [], [], [], []],
        timeList: [],
        title: '数量(次)',
        seriesData: [],
        colors: [],
      }
    },
    props:{
      chartData:{
        type: Array,
        default: () => {
          return []
        }
      }
    },
    created(){
      // 查询各企业 订单 总数趋势
      this.axios.get(process.env.API_HOST + '/index/selectEnterpriseOrderChart', {}).then(response => {
        let result = response.data
        if (result.code === 200) {
          result=result.result
          for(var i=0;i<result.length;i++){
            this.timeList.push(this.dtime(result[i].statisDate).format('MM-DD'))
            if (result[i].colletions.length > 0) {
              var colletions=result[i].colletions
              colletions=this.sortByKey(colletions, 'enterpriseNo')
              for(var k=0;k<colletions.length;k++){
                this.colors.push(this.$store.state.splitMapColors[colletions[k].enterpriseNo])
                if(i==0){
                  this.echartsNames.push(colletions[k].enterpriseName)
                }
                this.dataList[k].push(colletions[k].statisCount)
              }
            }
          }
          this.unique(this.echartsNames)
          this.initData()
        }
      })
    },
    methods: {
      disposeData(){
        //处理颜色
        // this.colors = []
        this.seriesData = []
        // this.$store.state.companyInfos.forEach(item => {
        //   this.colors.push(item.color)
        // })
        this.echartsNames.forEach((item, key) => {
          this.seriesData.push({
            name: item,
            type: 'line',
            symbol: 'circle',
            smooth: true,
            symbolSize: 5,
            showSymbol: false,
            data: this.dataList[key],
            markPoint: {
              data: [
                {type: 'max', name: '最大值'},
                // {type: 'min', name: '最小值'}
              ]
            },
          })
        })
      },
      initData(){
        this.disposeData()

        if (this.myChart != null && this.myChart != "" && this.myChart != undefined) {
          this.myChart.dispose();
        }

        this.myChart = this.echarts.init(document.getElementById('orderChart'))
        const option = {
          color: this.colors,
          title: {
            show: false,
            text: "",
            subtext: '',
            x: 'left',
            textStyle: {
              fontSize: 14,
              fontWeight: 'bolder',
              color: "#3975FE"        // 主标题文字颜色
            },
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
              type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            },

          },
          legend: {
            x: 'right',
            data: this.echartsNames,
            textStyle: {
              fontSize: 12,
              color: 'gray'          // 主标题文字颜色
            },
          },
          grid: {
            left: '1%',
            right: '1%',
            bottom: '4%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: this.timeList,
              axisTick: {
                alignWithLabel: true
              },
              textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                color: 'gray'
              },
              axisLine: {
                lineStyle: {
                  color: 'gray',
                }
              }
            }
          ],
          yAxis: [
            {
              type: 'value',
              name: this.title,
              min: 0,
              textStyle: {// 其余属性默认使用全局文本样式
                color: 'gray'
              },
              axisLine: {
                lineStyle: {
                  color: 'gray'
                }
              }
            }
          ],
          series: this.seriesData
        }
        this.myChart.setOption(option)
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../../common/stylus/mixin.styl"
  .orderChart
    padding 10px
    width: 100%
    height 100%;
    box-sizing border-box
    border-radius 15px
    text-align left
</style>